package table;

import java.io.File;
import javax.swing.table.AbstractTableModel;

class MyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private static final boolean DEBUG = false;

	// predefined variable by AbstractTableModel
	public Object[] longValues;
	private String[] columnNames = { "File Path", "Faile Name" };
	private Object[][] data;

	MyTableModel() {
		String srcFP = "x:\\dev\\temp\\";
		resetFileList(srcFP);
	}

	private void resetFileList(String srcFP) {
		File srcFile = new File(srcFP);

		// Vector<Object> tableDataSet= new Vector<Object>() ;

		if (srcFile.exists()) {
			File[] files = srcFile.listFiles();

			data = new Object[files.length][columnNames.length];

			int index = 0;
			for (int i = 0; i < files.length; i++) {
				if (!files[i].isDirectory()) {
					data[index][0] = new String(files[i].getParent());
					data[index][1] = new String(files[i].getName());
					index++;
				}
			}
		}
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	@Override
	public Object getValueAt(int row, int col) {
		return data[row][col];
	}

	/*
	 * JTable uses this method to determine the default renderer/ editor for
	 * each cell. If we didn't implement this method, then the last column would
	 * contain text ("true"/"false"), rather than a check box.
	 */
	@Override
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	@Override
	public boolean isCellEditable(int row, int col) {
		// Note that the data/cell address is constant,
		// no matter where the cell appears onscreen.
		if (col < 2) {

			return false;
		} else {
			return true;
		}
	}

	/*
	 * Don't need to implement this method unless your table's data can change.
	 */
	@Override
	public void setValueAt(Object value, int row, int col) {
		if (DEBUG) {
			System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of "
					+ value.getClass() + ")");
		}

		data[row][col] = value;
		fireTableCellUpdated(row, col);

		if (DEBUG) {
			System.out.println("New value of data:");
			printDebugData();
		}
	}

	private void printDebugData() {
		int numRows = getRowCount();
		int numCols = getColumnCount();

		for (int i = 0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j = 0; j < numCols; j++) {
				System.out.print("  " + data[i][j]);
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}
}